﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json.Linq;
using ST.Buz.Base.Entity;
using ST.Buz.Service;
using ST.Common.Util;
using ST.Common.Util.Model;
using ST.Common.Util.Mvc;
using ST.Common.Util.Text;
using System;
using System.Net;

namespace ST.Buz.Api.Controllers
{
	[Route("sys/area")]
	public class Sys_AreaController : BaseJsonController
	{
		private readonly IServiceSysArea service;
		public Sys_AreaController(IServiceSysArea _service,
			ILogger<Sys_AreaController> _logger) : base(_logger)
		{
			service = _service;
		}

		[HttpPost("ztree")]
		public IActionResult GetZTreeData([FromBody]JObject paras)
		{
			try
			{
				//UserSession sessionCurrent = GetRawSessionFromRequest();

				int id = TextHelper.GetInt(paras, "id");

				var result = service.GetSimpleTreeData(id);

				return new JsonResultBase(result);
			}
			catch (AuthException ex)
			{
				return new JsonResultBase(false, ex.Message, (int)HttpStatusCode.Unauthorized);
			}
			catch (BuzException ex)
			{
				return new JsonResultBase(false, ex.Message);
			}
			catch (ArgumentException ex)
			{
				logger.LogError(ex.InnerException ?? ex, ex.Message);
				return new JsonResultBase(false, ex.Message, (int)HttpStatusCode.BadRequest);
			}
			catch (Exception ex)
			{
				logger.LogError(ex.InnerException ?? ex, ex.Message);
				return new JsonResultBase(false, "请求出错", (int)HttpStatusCode.InternalServerError);
			}
		}
	}
}